package com.asion.gankapp.model;

import com.asion.gankapp.entrys.BaseEntry;
import com.asion.gankapp.servers.ServiceApi;
import com.asion.gankapp.utils.SetCacheUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by asion on 16-10-6.
 */

public class SearchModel implements BaseModel {

    public void getQurryData(String qurry, int page,AllTypeModel.OnGetEntryDatas onGetEntryDatas){
        new Retrofit.Builder().baseUrl("http://gank.io/api/").addConverterFactory(GsonConverterFactory.create())
                .client(SetCacheUtils.setCache()).build().create(ServiceApi.class).getSearchCommend(qurry,page)
                .enqueue(new Callback<BaseEntry>() {
                    @Override
                    public void onResponse(Call<BaseEntry> call, Response<BaseEntry> response) {
                        onGetEntryDatas.onGetEntrySuccess(response.body());
                    }

                    @Override
                    public void onFailure(Call<BaseEntry> call, Throwable t) {
                        onGetEntryDatas.onGetEntryError("数据请求失败，请检查网络...");
                    }
                });
    }

    public void getRandomData(AllTypeModel.OnGetEntryDatas onGetEntryDatas){
        new Retrofit.Builder().baseUrl("http://gank.io/api/").addConverterFactory(GsonConverterFactory.create())
                .client(SetCacheUtils.setCache()).build().create(ServiceApi.class).getRandomCommend()
                .enqueue(new Callback<BaseEntry>() {
                    @Override
                    public void onResponse(Call<BaseEntry> call, Response<BaseEntry> response) {
                            onGetEntryDatas.onGetEntrySuccess(response.body());
                    }

                    @Override
                    public void onFailure(Call<BaseEntry> call, Throwable t) {
                            onGetEntryDatas.onGetEntryError("数据请求失败,请检查网络...");
                    }
                });
    }
}
